<?php

namespace Repository;

use Doctrine\ORM\EntityRepository;

class CategoryRepository extends EntityRepository {
	
	public function addCate($data){
		$cate = new \Entity\ShCategory();

		var_dump($cate);
		$cate->setName($data->post('name'));
		$cate->setActive($data->post('active'));
		$cate->setSort($data->post('sort'));

		$this->_em->persist($cate);
		$this->_em->flush();
		return true;
	}
	public function updateCate($data,$cate){
		
		$cate->setName($data->post('name'));
		$cate->setActive($data->post('active'));
		$cate->setSort($data->post('sort'));

		$this->_em->persist($cate);
		$this->_em->flush();
		return true;
	}
	public function deleteCate($cate){
		$data = $this->_em->getRepository('Entity\ShCategory')->findOneById($cate);
		$this->_em->remove($data);
		$this->_em->flush();
		return true;
	}
	public function backendPaginate($conf){
		$q = $this->_em->getRepository('\Entity\ShCategory')->findBy(
			$conf['condition'], $conf['order'], $conf['limit'], $conf['offset']
			);
		return $q;
	}
	
	public function frontPaginate($cat, $pag){
		$q = $this->_em->getRepository('\Entity\ShProduct')->findBy(array(
				'category' => $cat,
				'status' => 1
			),array($pag['sortBy'] => $pag['sortOrder']), $pag['limit'], $pag['offset']
		);
		return $q;
	}
	
	public function total_rows($pag){
		$q = $this->_em->createQueryBuilder()
		               ->select('COUNT(cate.id)')
		               ->from('Entity\ShCategory','cate');
		return $q->getQuery()->getSingleScalarResult();
	}
	
	public function front_total_rows($cat){
		$q = $this->_em->createQueryBuilder()
		               ->select('COUNT(p.id)')
		               ->from('Entity\ShProduct','p')
					   ->where('p.status = 1')
					   ->andWhere('p.category = '.$cat);
		return $q->getQuery()->getSingleScalarResult();
	}
}